<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>评论回车发布</title>
  <link rel="stylesheet" href="index.css">
  <link rel="stylesheet" href="common.css">
  <link rel="stylesheet" href="base.css">
  <style>
    .wrapper1 {
      min-width: 400px;
      max-width: 800px;
      display: flex;
      justify-content: flex-end;
      margin-top: 60px;
      margin-left: 160px;
    }

    .avatar {
      width: 48px;
      height: 48px;
      border-radius: 50%;
      overflow: hidden;
      background: url(img/用户-角色-用户名-单人_jurassic.jpg) no-repeat center / cover;
      margin-right: 20px;
    }

    .wrapper1 textarea {
      outline: none;
      border-color: transparent;
      resize: none;
      background: #f5f5f5;
      border-radius: 4px;
      flex: 1;
      padding: 10px;
      transition: all 0.5s;
      height: 65px;
    }

    .wrapper1 textarea:focus {
      border-color: #e4e4e4;
      background: #fff;
      height: 80px;
    }

    .wrapper1 button {
      background: #00aeec;
      color: #fff;
      border: none;
      border-radius: 4px;
      margin-left: 10px;
      width: 70px;
      cursor: pointer;
    }

    .wrapper1 .total {
      margin-right: 80px;
      color: #999;
      margin-top: 5px;
      opacity: 0;
      transition: all 0.5s;
    }

    .list {
      min-width: 400px;
      max-width: 800px;
      display: flex;
    }

    .list .item {
      width: 100%;
      float: left;
      margin-left: 20%;
    }

    .list .item .info {
      flex: 1;
      border-bottom: 1px dashed #e4e4e4;
      padding-bottom: 10px;
    }

    .list .item p {
      margin: 0;
    }

    .list .item .name {
      color: #FB7299;
      font-size: 14px;
      font-weight: bold;
    }

    .list .item .text {
      color: #333;
      padding: 10px 0;
    }

    .list .item .time {
      color: #999;
      font-size: 12px;
    }
  </style>
</head>

<body>
<div class="shortcut">
  <div class="wrapper">
    <i class="avatar"></i>
    <ul>
      <li><a href="登录login.html" id="top">您好！请先登录</a></li>
      <li><a href="注册导航.html">免费注册</a></li>
      <li><a href="index.html">主页</a></li>
      <li><a href="ShopCar.html">购物车</a></li>
      <li><a href="#">收货中心</a></li>
      <li><a href="homePage.html">个人中心</a></li>
      <li><a href="视觉介绍.html"><span></span>官网</a></li>
    </ul>
  </div>
</div>
<div class="header wrapper" id="header-pic">
  <div class="logo">
    <h1><a href="#">企业图片</a></h1>
  </div>
  <div class="nav">
    <ul>
      <li> <a href="#">首页</a> </li>
      <li> <a href="#">电子产品</a> </li>
      <li> <a href="#">玩具手办</a> </li>
      <li> <a href="#">服装和鞋帽</a> </li>
      <li> <a href="#">运动和户外用品</a> </li>
      <li> <a href="#">服务中心</a> </li>
    </ul>
  </div>
  <div class="search">
    <input type="text" placeholder="手办">
  </div>
  <div class="car">
    <a href="#"><img src="img/R-C.png" class="car-pic" href="#"></a>
  </div>
</div>
<div class="wrapper1">
  <i class="avatar"></i>
  <textarea id="tx" placeholder="发一条友善的评论" rows="2" maxlength="200"></textarea>
  <button>发布</button>
</div>
<div class="wrapper1">
  <span class="total">0/200字</span>
</div>
<div class="list">
  <div class="item" style="display: none;">
    <i class="avatar"></i>
    <div class="info">
      <p class="name">清风徐来</p>
      <p class="text">大家都辛苦啦，感谢各位大大的努力，能圆满完成真是太好了[笑哭][支持]</p>
      <p class="time">2022-10-10 20:29:21</p>
    </div>
  </div>
</div>
<script>
  const text =document.querySelector('#tx')
  const total =document.querySelector('.total')
  const item=document.querySelector('.item')
  const t=document.querySelector('.text')
  const name=document.querySelector('.name')
  const time=document.querySelector('.time')
  const btn=document.querySelector('button')
  const dater=localStorage.getItem('YM')

  console.log(dater)
  text.addEventListener('focus',function (){
      total.style.opacity= '1'
  })
  text.addEventListener('blur',function (){
    total.style.opacity= '1'
  })
  text.addEventListener('keydown',function (){
    total.innerHTML=`${text.value.length}/200字`
  })
  text.addEventListener('keyup',function (e){
    e.preventDefault()
    if (!dater){
      alert('请先登录再评论！')
      return false
    }
    if (e.key==='Enter'){
      if (text.value.trim()!==''){
        item.style.display='block'
        t.innerHTML=text.value
        time.innerHTML=new Date().toLocaleString()
        name.innerHTML=dater
      }
    }
  })
  btn.addEventListener('click',function () {
    if (!dater){
      alert('请先登录再评论！')
      return false
    }
       if (text.value.trim() !== '') {
        item.style.display = 'block'
        t.innerHTML = text.value
         time.innerHTML=new Date().toLocaleString()
         name.innerHTML=dater
      }
  })
  const FirstLi=document.querySelector('.shortcut ul li:nth-child(1)')
  const SecondLi=FirstLi.nextElementSibling
  function render(){
    const uname=JSON.parse(localStorage.getItem('YM'))
    console.log(uname)
    if (uname){
      FirstLi.innerHTML=`<a href="javascript:;"><i class="iconfont"><img src="img/用户-角色-用户名-单人_jurassic.jpg" alt="" height="30">${uname}</i></a>`
      SecondLi.innerHTML=`<a href="javascript:;">退出登录</a>`
    }
    else{
      FirstLi.innerHTML=`<a href="登录login.html">您好！请先登录</a>`
      SecondLi.innerHTML=`<a href="注册导航.html">免费注册</a>`

    }
  }
  render()
  SecondLi.addEventListener('click',function (){
    localStorage.removeItem('YM')
    alert('退出成功')
    render()
  })
  const ShopCar=document.querySelector('.wrapper li:nth-child(4) a')
  ShopCar.addEventListener('click',function (e){
    e.preventDefault()
    const uname=JSON.parse(localStorage.getItem('YM'))
    if (!uname){
      alert('请先登录！')
    }

  })
</script>
</body>

</html>